<?php 
defined('IN_IXDCW') or exit('Access Denied');

class GuestbookModule extends BasicModule{
	private $_itemid;
	private $_table;
	private $_fields;

    function __construct() {
	    parent::__construct();
		global $db;
		$this->_table = $db->getPre().'guestbook';
		$this->_fields = array( 'title','areaid','content','truename','telephone','email','qq','msn','ali','skype','hidden','status','username','addtime', 'ip', 'reply','editor','edittime');
    }

	public function setItemid($itemid) {
		$this->_itemid = $itemid;
	}

	public function getItemid() {
		return $this->_itemid;
	}

	public function getFields() {
		return $this->_fields;
	}

	function pass($post) {
		global $L;
		if(!is_array($post)) return false;
		if(!$post['content']) return $this->err($L['gbook_pass_content']);
		return true;
	}

	function set($post) {
		global $DT_TIME, $_username, $DT_IP, $TYPE;
		$post['content'] = trim(strip_tags($post['content']));
		$post['title'] = in_array($post['type'], $TYPE) ? '['.$post['type'].']' : '';
		$post['title'] .= Str::cut($post['content'], 30);
		$post['hidden'] = isset($post['hidden']) ? 1 : 0;
		if($this->_itemid) {
			$post['status'] = $post['status'] == 2 ? 2 : 3;
			$post['editor'] = $_username;
			$post['edittime'] = $DT_TIME;
			$post['reply'] = trim($post['reply']);
		} else {
			$post['username'] = $_username;
			$post['addtime'] =  $DT_TIME;
			$post['ip'] =  $DT_IP;
			$post['edittime'] = 0;
			$post['reply'] = '';
			$post['status'] = 2;
		}
		$post = Str::htmlSpecialChars($post);
		return $post;
	}

	function getOne() {
        return $this->_db->getOne("SELECT * FROM {$this->_table} WHERE itemid='$this->_itemid'");
	}

	function getList($condition = 'status=3', $order = 'itemid DESC') {
		global $MOD,$items;
		$num = $this->_db->count($this->_table, $condition, $cache);
		$items=$num;
		$this->_pn->recordsPerPage($this->_pagesize);
		$this->_pn->records($num);
		$sql = "SELECT * FROM {$this->_table}";
		$sql .= $condition ? " WHERE {$condition}" : '';
		$sql .= $order ? " ORDER BY {$order}" : '';
		$sql .= " LIMIT " . ($this->_pn->getPage() - 1) * $this->_pagesize . ',' . $this->_pagesize;
		$lists = array();
		$result = $this->_db->query($sql);
		while($r = $this->_db->fetchArray($result)) {
			$r['adddate'] = timetodate($r['addtime'], 5);
			$r['content'] = nl2br($r['content']);
			$r['editdate'] = '--';
			if($r['reply']) {
				$r['reply'] = nl2br($r['reply']);
				$r['editdate'] = timetodate($r['edittime'], 5);
			}
			$lists[] = $r;
		}
		return $lists;
	}

	function add($post) {
		$post = $this->set($post);
		$this->_db->query(Sql::insert($this->_table, $post, $this->_fields));
		return $this->_itemid;
	}

	function edit($post) {
		$post = $this->set($post);
		$this->_db->query(Sql::update($this->_table, $post, "itemid={$this->_itemid}", '', $this->_fields));
		return true;
	}

	function delete($itemid) {
		if(is_array($itemid)) {
			foreach($itemid as $v) { $this->delete($v); }
		} else {
			$this->_db->query(Sql::delete($this->_table,"itemid=$itemid"));
		}
	}

	function check($itemid, $status) {
		if(is_array($itemid)) {
			foreach($itemid as $v) { $this->check($v, $status); }
		} else {
			$this->_db->query(Sql::update($this->_table,"status=$status","itemid=$itemid"));
		}
	}

}
?>